home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / XMODEM.ZIP / xmodmcrc.txt < prev   
Text File  |  1996-03-06  |  5KB  |  140 lines

  1. Perception presents:
  2. ---------- Understanding The X-Modem CRC File Transfer Protocol --------------
  3.  
  4.                   by Em Decay
  5.  
  6. This has to be one of the most internationally accepted protocols for upload-
  7.  ing and downloading binary and text files.  It is fairly straight-forward as
  8.  to how it is set up and there are some error checking capablities.
  9.  
  10.  
  11. --- Before you begin ---
  12.  
  13. Look at my XMODEM.TXT text file for a general understanding of the X-Modem
  14.  file transfer protocol and the terms used in it.
  15.  
  16. New things you need to know beforehand...
  17.  
  18. One word is the same as two bytes.  If you want the high byte and the low
  19.  byte of a word, simply do the following.
  20.    High byte = word DIV 256   (DIV is the same as divide except the answer is  
  21.                   truncated)
  22.    Low byte = $00ff AND word  (AND refers to AND logic)
  23.  
  24. To get a word from a high byte and a low byte, simply multiply the high byte
  25.  by 256 (or shift it left 8 bits, if you know assembly) and add the low byte
  26.  to it.
  27.  
  28. CRC stands for Cyclical Redundancy Check.  In X-Modem CRC, it is also refer-
  29.  red to as CRC-16 since there are 16 bits (1 word) at the end of the block
  30.  that contain the CRC.  This 1 word CRC replaces the 1 byte checksum in
  31.  X-Modem.
  32. CRC-16 guarantees detection of all single and double bit errors, all errors
  33.  with an odd number of bits and over 99.9969% of all burst errors (you don't
  34.  have to know what all these things are :). 
  35. The easiest and fastest way to calculate the CRC is to use a lookup table.
  36.  
  37. Here is some source code about making a lookup table.  Call this procedure
  38.  at the very beginning of the program.  Make crctable an global variable.
  39.  It is an array [0..255] of word.
  40.  
  41.  procedure initcrc;
  42.  
  43.    var
  44.      i:integer;
  45.  
  46.    function calctable(data,genpoly,accum:word):word;
  47.  
  48.      var
  49.        j:word;
  50.  
  51.      begin
  52.        data:=data shl 8;
  53.        for j:=8 downto 1 do
  54.      begin
  55.        if ((data xor accum) and $8000)<>0 then
  56.          accum:=(accum shl 1) xor genpoly
  57.        else
  58.          accum:=accum shl 1;
  59.        data:=data shl1;
  60.      end;
  61.        calctable:=accum;
  62.      end;
  63.  
  64.    begin
  65.      for i:=0 to 255 do
  66.        crctable[i]:=calctable(i,4129,0);
  67.    end;
  68.  
  69. The CRC starts with a value of zero at the beginning of each block.  Now to 
  70.  update the CRC, with each byte in the 128 byte packet simply do this (the
  71.  oldcrc is the crc value to be updated, data is the current byte):
  72.      CRC:=(oldcrc shl 8) xor (crctable[(oldcrc shr 8) xor data]);
  73. The final value of the CRC (after all 128 bytes) is what is being sent in the 
  74.  X-Modem CRC protocol.
  75.  
  76. If you have somewhat understood what has just been described then you catch
  77.  on a lot faster than I did :)  If you just use
  78.  
  79.  
  80. --- The Actual Transfer ---
  81.  
  82. As in X-Modem, the uploader waits for the downloader to send the NCGbyte.  
  83.  The NCGbyte for X-Modem CRC is chr(67) or the capital letter C (unlike 
  84.  X-Modem where the NCGbyte is chr(21), the NAK).  If the downloader takes too 
  85.  long or an error occurs then the uploader will stop waiting or "Time Out".
  86.  If this happens, then the file transfer must restart.
  87.  
  88. With each packet sent...
  89.  
  90.     The uploader sends:
  91.  
  92.     1. an SOH byte                             {1 byte}
  93.     2. the packet number                       {1 byte}
  94.     3. the 1's complement of the packet number {1 byte}
  95.     4. the packet                            {128 bytes}
  96.     5. the high byte of the CRC-16             {1 byte}
  97.     6. the low byte of the CRC-16              {1 byte}
  98.  
  99.     These six things make up the block.
  100.  
  101.     The downloader:
  102.  
  103.     1. ensures that the packet number sent matches the actual packet number
  104.      that it is (If the third block sent has a '4' as the second byte,
  105.      something is wrong --> CANCEL TRANSFER (send CAN byte))
  106.     2. adds the packet number and the 1's complement of the packet number
  107.      together to make sure that they add up to 255.  if they don't -->
  108.      CANCEL TRANSFER
  109.     3. sets the CRC to zero
  110.     4. updates the CRC as each byte in the packet is sent
  111.     5. compares the calculated CRC to the CRC that is sent
  112.     6. if everything looks ok (calculated CRC=sent CRC), then the downloader
  113.      appends the bytes in the packet to the file being created (sent).
  114.      The downloader then sends an ACK byte which tells the uploader to
  115.      send the next block.
  116.        if the CRCs do not match, then the downloader sends a NAK byte which
  117.      tells the uploader to send the same block it just sent over again.
  118.  
  119. When the uploader sends an EOT byte instead of an SOH byte, the downloader
  120.  sends a NAK byte.  If the uploader sends another EOT immediately after that,
  121.  the downloader sends an ACK byte and the transfer is complete.
  122.  
  123. Another thing, the downloader can cancel the transfer at any time by sending
  124.  a CAN byte.  The uploadered can cancel only between blocks by sending a CAN
  125.  byte.  It is recommended that you send between 2 and 8 consecutive CAN bytes
  126.  as some communication programs will not allow you to cancel with only 1 CAN
  127.  byte.
  128.  
  129.  
  130. --- Wrap Up ---
  131.  
  132. Hopefully, you were able to follow along. :)  If not, you can e-mail me at
  133.  em_decay@norlink.net  and I will try to clarify it for you.  Have fun :)
  134.  
  135. Perception:    Em Decay -- Mark Korhonen
  136.            Cmf ------- Chris Fillion
  137.  
  138. Written on Jan.3/95
  139.  
  140.